//
//  XDAssetDetailsViewController.m
//  xindailianmeng
//
//  Created by 张东 on 2017/3/14.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDAssetDetailsViewController.h"
#import "XDAssetDetailView.h"
#import "ZHRTRequestFailureShowViewOrDetail.h"
#import "AFHTTPSessionManager+POST.h"
#import "XDLMLoginViewController.h"
#import "AppDelegate.h"
#import "XDAssetDetails.h"
#import "MJExtension.h"
@interface XDAssetDetailsViewController ()
@property (nonatomic, weak) UIView * errorView;

@property (nonatomic, weak) UIView *progressHUDView;

@property (nonatomic, strong) XDAssetDetails *asset;

@property (nonatomic, weak) XDAssetDetailView *assetview;
@end

@implementation XDAssetDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"总资产";
    self.view.backgroundColor = kColorRGB(234, 234, 234);
    XDAssetDetailView *view = [[XDAssetDetailView alloc] initWithFrame:kScreenB];
    self.assetview = view;
    self.view = view;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadData];
    [self navigationBarCof];
}



- (void)navigationBarCof {
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"transparentNav"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (void)loadData {
    
    [ZHRTRequestFailureShowViewOrDetail showProgressHUDWithInView:self.progressHUDView type:1];
    NSDictionary * param = @{@"method":@"assetDetails"};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
        [self.progressHUDView removeFromSuperview];
        
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            
            [self.errorView removeFromSuperview];
//            code = 100000;
//            message = "\U64cd\U4f5c\U6210\U529f";
//            result =     {
//                avaMoney = 0;
//                sanBiao = 0;
//                totalMoney = 0;
//                youXuan = 0;
//            };
//            type = success;
//        }

            self.assetview.assetDic = [XDAssetDetails mj_objectWithKeyValues:responseObject[@"result"]];
        } else if ([responseObject[@"code"] isEqualToString:@"100054"] ||[responseObject[@"code"] isEqualToString:@"100001"]){ // 用户信息错误
            XDLMLoginViewController *loginVc = [[XDLMLoginViewController alloc] init];
            loginVc.hidesBottomBarWhenPushed = YES;
            loginVc.isTip = YES;
            NSUserDefaults *df =[NSUserDefaults standardUserDefaults];
            [df setValue:@"Y" forKey:@"isMine"];
            [df removeObjectForKey:@"uid"];
            [df synchronize];
            XDLMNavigatonController * nav = [[XDLMNavigatonController alloc] initWithRootViewController:loginVc];
            AppDelegate *delegate =(AppDelegate *) [UIApplication sharedApplication].delegate;
            [delegate.window.rootViewController presentViewController:nav animated:YES completion:nil];
            
        } else {
            
            [ZHRTRequestFailureShowViewOrDetail showViewDetail:responseObject[@"message"] view:self.view];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [self.progressHUDView removeFromSuperview];
        if (!_errorView) {
            
            [self.view addSubview:self.errorView ];
            [ZHRTRequestFailureShowViewOrDetail showRequestFailureForView:_errorView target:self action:@selector(loadData) type:1];
        }
    }];

}


- (UIView *)errorView {
    if (_errorView == nil) {
        UIView *view = [[UIView alloc] initWithFrame:kScreenB];
        _errorView = view;
        [self.view addSubview:view];
    }
    return _errorView;
}
- (UIView *)progressHUDView {
    if (_progressHUDView == nil) {
        UIView *view = [[UIView alloc] initWithFrame:kScreenB];
        _progressHUDView = view;
        [self.view addSubview:view];
    }
    return _progressHUDView;
}

@end
